package com.ruyuan.commodity.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruyuan.commodity.model.goods.dos.GoodsSkuDO;
import com.ruyuan.commodity.model.goods.vo.GoodsSkuVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * GoodsSku的Mapper
 */
@Repository
public interface GoodsSkuMapper extends BaseMapper<GoodsSkuDO> {

    /**
     * 查询sku库存
     * @param skuId skuId
     * @return
     */
    @ResultType(Map.class)
    @Select("select enable_quantity,quantity from es_goods_sku where sku_id=#{sku_id}")
    Map<String, Integer> queryQuantity(@Param("sku_id") Long skuId);

    /**
     * 更新sku库存
     * @param param 条件
     */
    @Update("update es_goods_sku set enable_quantity=#{param.enable_quantity}, quantity=#{param.quantity} where sku_id=#{param.sku_id}")
    void updateQuantity(@Param("param") Map param);

    /**
     * 查询sku的list集合
     * @param skuIdList skuid集合
     * @return
     */
    @Select("<script>" +
            "select * from es_goods_sku " +
            "        <where>" +
            "            <if test='params != null'>" +
            "               sku_id in" +
            "                <foreach collection='params' index='index' item='item' open='(' separator=',' close=')'>" +
            "                   #{item}" +
            "                </foreach>" +
            "            </if>" +
            "        </where>" +
            "</script>")
    List<GoodsSkuVO> queryGoodsSkuVOList(@Param("params") List<Long> skuIdList);

    /**
     * 查询某商家的上架，审核通过的sku集合
     * @param sellerId 商家id
     * @return
     */
    @Select("select s.* from es_goods_sku s inner join es_goods g on s.goods_id = g.goods_id " +
            "where s.seller_id = #{seller_id} and g.market_enable = 1 and g.disabled = 1 and g.is_auth = 1")
    List<GoodsSkuDO> querySellerAllSku(@Param("seller_id") Long sellerId);
}
